import { login, getInfo } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
// import { reject } from 'core-js/fn/promise'
// import { resolve } from 'core-js/fn/promise'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token // 设置token
      setToken(token)
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeUserInfo(state) {
      state.token = ''
      state.userInfo = {}
      removeToken()
    }
  },

  actions: {
    // data表示接口参数
    asyncSetToken(ctx, data) {
      return new Promise((resolve, reject) => {
        login(data).then(res => {
          // 提交mutation函数
          ctx.commit('setToken', res)
          resolve(res)
        })
      })
    },
    asyncGetUserInfo(ctx) {
      return new Promise((resolve, reject) => {
        getInfo().then((res) => {
          ctx.commit('setUserInfo', res)
          resolve(res)
        })
      })
    }
  }
}

